package com.stu.boke.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stu.boke.entity.User;
import com.stu.boke.mapper.ArticleMapper;
import com.stu.boke.mapper.UserMapper;
import com.stu.boke.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired(required = false)
	private UserMapper userMapper;

	@Autowired(required = false)
	private ArticleMapper articleMapper;

	@Override
	public List<User> listUser() {
		List<User> userList = userMapper.listUser();
		for (int i = 0; i < userList.size(); i++) {
			Integer articleCount = articleMapper.countArticleByUser(userList.get(i).getUserId());
			userList.get(i).setArticleCount(articleCount);
		}
		return userList;
	}

	@Override
	public User getUserById(Integer id) {
		return userMapper.getUserById(id);
	}

	@Override
	public void updateUser(User user) {
		userMapper.update(user);
	}

	@Override
	public void deleteUser(Integer id) {
		userMapper.deleteById(id);
	}

	@Override
	public User insertUser(User user) {
		user.setUserRegisterTime(new Date());
		userMapper.insert(user);
		return user;
	}

	@Override
	public User getUserByNameOrEmail(String str) {
		return userMapper.getUserByNameOrEmail(str);
	}

	@Override
	public User getUserByName(String name) {
		return userMapper.getUserByName(name);
	}

	@Override
	public User getUserByEmail(String email) {
		return userMapper.getUserByEmail(email);
	}


}
